﻿using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Kenly.UTAssist;

namespace Kenly.DBFramework.Tests
{


    /// <summary>
    ///这是 PagingArgTest 的测试类，旨在
    ///包含所有 PagingArgTest 单元测试
    ///</summary>
    [TestClass()]
    public class PagingArgTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///MinRowNumber 的测试
        ///</summary>
        [TestMethod()]
        public void MinRowNumberTest()
        {
            string msg = "PagingArg.MinRowNumber did not return the expected value.pageIndex={0},pageSize={1}";

            int pageIndex = 0;
            int pageSize = 0;

            int actual = PagingArg.MinRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 0, msg);

            pageSize = -10;
            actual = PagingArg.MinRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 0, GetMessage(msg, pageIndex, pageSize));

            pageSize = 10;
            actual = PagingArg.MinRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 1, GetMessage(msg, pageIndex, pageSize));

            pageIndex = 1;
            actual = PagingArg.MinRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 11, GetMessage(msg, pageIndex, pageSize));
        }

        private static string GetMessage(string msg, int pageIndex, int pageSize)
        {
            return string.Format(msg,
                pageIndex.ToString(), pageSize.ToString());
        }

        /// <summary>
        ///MaxRowNumber 的测试
        ///</summary>
        [TestMethod()]
        public void MaxRowNumberTest()
        {
            string msg = "PagingArg.MaxRowNumber did not return the expected value.pageIndex={0},pageSize={1}";

            int pageIndex = 0;
            int pageSize = 0;

            int actual = PagingArg.MaxRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 0, msg);

            pageSize = -10;
            actual = PagingArg.MaxRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 0, GetMessage(msg, pageIndex, pageSize));

            pageSize = 10;
            actual = PagingArg.MaxRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 10, GetMessage(msg, pageIndex, pageSize));

            pageIndex = 1;
            actual = PagingArg.MaxRowNumber(pageIndex, pageSize);
            Assert.IsTrue(actual == 20, GetMessage(msg, pageIndex, pageSize));
        }

        /// <summary>
        ///CalculatePageCount 的测试
        ///</summary>
        [TestMethod()]
        public void CalculatePageCountTest()
        {
            string msg = "PagingArg.MaxRowNumber did not return the expected value.totalRowCount={0},pageSize={1}";

            int totalRowCount = 0;
            int pageSize = 0;

            int actual = PagingArg.CalculatePageCount(totalRowCount, pageSize);
            Assert.IsTrue(actual == 0, GetMessage(msg, totalRowCount, pageSize));

            totalRowCount = 99;
            actual = PagingArg.CalculatePageCount(totalRowCount, pageSize);
            Assert.IsTrue(actual == 0, GetMessage(msg, totalRowCount, pageSize));

            pageSize = -10;
            actual = PagingArg.CalculatePageCount(totalRowCount, pageSize);
            Assert.IsTrue(actual == 0, GetMessage(msg, totalRowCount, pageSize));

            pageSize = 10;
            actual = PagingArg.CalculatePageCount(totalRowCount, pageSize);
            Assert.IsTrue(actual == 10, GetMessage(msg, totalRowCount, pageSize));

        }


        /// <summary>
        ///CalculatePageCount 的测试
        ///</summary>
        [TestMethod()]
        public void ToStringTest()
        {
            AssertHelper helper = new AssertHelper(typeof(PagingArg), "ToString");

            PagingArg pagingArg = new PagingArg(0, 20);

            Assert.IsNotNull(pagingArg.ToString(), helper.BuildMessage(pagingArg.ToString()));
        }


    }
}
